package com.myframe.demo.servlet;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import com.common.doc.xsl.CellManager;
import com.common.doc.xsl.ICellCommander;
import com.common.doc.xsl.POIFManager;
import com.myframe.demo.vo.DbTest;

public class ExcelServlet extends HttpServlet {
	
	
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		try{
			//List list = (List) this.getSession().getAttribute("fcList");
			String file = req.getSession().getServletContext().getRealPath("")
					+ "\\xls\\gjfc.xls";

			String fileout = req.getSession().getServletContext().getRealPath("")+ "\\out\\gjfc.xls";

			POIFManager _poif = POIFManager.getCell4T(file);
			List list = new ArrayList();
			_poif.iteratorValue4XLS(list, _poif.getSheet(0), 4,
					new ICellCommander()
					{

						public void doInIterator(Object data, HSSFSheet sheet,
								short row, int index)
						{

							DbTest bean = (DbTest) data; // 数据格式化

							HSSFCell _cell = POIFManager.createCell(sheet, row,
									0);
							_cell.setCellValue(index + 1);
							_cell = POIFManager.createCell(sheet, row, 1);
							_cell.setCellValue(new HSSFRichTextString(bean.getId() == null ? ""
									: bean.getId().toString()));
							_cell = POIFManager.createCell(sheet, row, 2);
							_cell.setCellValue(new HSSFRichTextString(bean.getName() == null ? ""
									: bean.getName().toString()));
							_cell = POIFManager.createCell(sheet, row, 3);
							_cell.setCellValue(new HSSFRichTextString(bean.getIntFeild() == null ? "" : bean
									.getIntFeild().toString()));
						}
					});

			HSSFCell _cell1 = CellManager.createTitleNoStyle(_poif.getSheet(0), 2, 2);
			_cell1.setCellValue("");
			
			_poif.writ2disk(fileout);
			
			
			OutputStream output = res.getOutputStream();
			byte b[] = new byte[1024];
			File fileLoad = new File(fileout);
			String docName = "工具、仪器和仪表封存台账.xls";
			docName = java.net.URLEncoder.encode(docName, "UTF-8");
			res.setHeader(
					"Content-Disposition",
					"attachment;filename="
							+ new String(docName.getBytes("UTF-8"), "GBK"));
			res.setContentType("application/vnd.ms-excel");
			long fileLength = fileLoad.length();
			String length = String.valueOf(fileLength);
			res.setHeader("Content_Length", length);
			FileInputStream input = new FileInputStream(fileLoad);
			int n = 0;
			while ((n = input.read(b)) != -1)
			{
				output.write(b, 0, n);
			}
		} catch (Exception e)
		{

			e.printStackTrace();
		}
	}
	
}
